﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;

namespace DAL
{
   public class CarInfoDAL
    {
        /// <summary>
        /// 查询商品绑定数据
        /// </summary>
        /// <returns></returns>
        public DataTable Dat()
        {
            string ss = "select * from View_1";
            DataTable da = new DB().Dat(ss);
            return da;
        }
        /// <summary>
        /// 车辆类别查询
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public DataTable Tiao(int name)
        {
            string ss = "select * from View_1 where  TypeID = " + name;
            DataTable da = new DB().Dat(ss);
            return da;
        }
        public bool Shan(int id)
        {
            string ss = "DELETE FROM[dbo].[CarInfo] WHERE CarID ="+ id;
            return new DB().ZSG(ss);
        }
        public bool Insert(CarInfoModels c) {
            string ss = string.Format("INSERT INTO [dbo].[CarInfo]([CarName],[CarPrice] ,[Place] ,[Desc],[TypeID]) VALUES('{0}','{1}','{2}','{3}','{4}')",c.CarName,c.CarPrice,c.Place,c.Desc,c.TypeID);
            return new DB().ZSG(ss);
        }

        public DataTable TiaoJian(string name)
        {
            string ss =string.Format( "select * from View_1 where CarName= '{0}'",name) ;
            DataTable da = new DB().Dat(ss);
            return da;
        }
        public CarInfoModels Car(string s) {
            string ss = "select * from carinfo where carid = " + s;
            DataTable dat = new DB().Dat(ss);
            if (dat.Rows != null && dat.Rows.Count >0)
            {
                CarInfoModels c = new CarInfoModels() {
                    CarID =Convert.ToInt32( dat.Rows[0]["carid"]),
                    CarName = dat.Rows[0]["carname"]+"",
                    CarPrice =Convert.ToInt32(dat.Rows[0]["carprice"]),
                    Place = dat.Rows[0]["place"] + "",
                    Desc = dat.Rows[0]["desc"]+"",
                    TypeID = Convert.ToInt32( dat.Rows[0]["typeid"])
                };
                return c;
            }
            else
            {
                return null;
            }
        }
        public bool Update(CarInfoModels c ,string s)
        {
            string ss = string.Format("UPDATE [dbo].[CarInfo] SET [CarName] = '{0}',[CarPrice] ='{1}',[Place] = '{2}',[Desc] = '{3}',[TypeID] = '{4}'WHERE CarID='{5}'",c.CarName,c.CarPrice,c.Place,c.Desc,c.TypeID,s);
            return new DB().ZSG(ss);
        }
    }
}
